import 'package:flutter/material.dart';
import 'package:ninetyfour/l10n/app_localizations.dart';
import 'package:package_info_plus/package_info_plus.dart';
import '../../components/toast_utils.dart';
import '../../login/controller/webview.dart';
import '../../manger/global_config.dart';
import '../../services/version_manger.dart';
import '../view/newversion_view.dart';
class AboutPage extends StatelessWidget {
  const AboutPage({super.key});

  List<String> getOptions(BuildContext context) {
    return [
      AppLocalizations.of(context)!.banbengengxin,
      AppLocalizations.of(context)!.yinsixieyi,
      AppLocalizations.of(context)!.fuwuxieyi
    ];
  }

  void checkVersion(BuildContext context) {
    // TODO: 实现版本检查逻辑
    GetVersionManager.shared.getNewVersion((reminderType, isShow) async {
      if (reminderType == 1 && isShow) {
        showDialog(
        context: context,
        barrierDismissible: false,
        builder: (_) => NewVersionView(model: GetVersionManager.shared.newModel!),
      );
      } else {
        ToastUtils.showCenterToast(context, AppLocalizations.of(context)!.isNewVersion);
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    final options = getOptions(context);
    final packageInfo = PackageInfo.fromPlatform();

    return Scaffold(
      appBar: AppBar(
        toolbarHeight: 40,
        title: Text(AppLocalizations.of(context)!.aboutUs, style: const TextStyle(fontSize: 17)),
      ),
      body: SingleChildScrollView(  // 使用 SingleChildScrollView 确保页面可滚动
        child: Column(
          children: [
            Container(
              margin: const EdgeInsets.only(top: 20, bottom: 20),
              padding: const EdgeInsets.all(20),
              width: MediaQuery.of(context).size.width - 30,
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(12),
              ),
              child: Column(
                children: [
                  Image.asset('assets/images/applogo.png', width: 80, height: 80),
                  const SizedBox(height: 20),
                  Text('94LIVES.COM', style: TextStyle(fontSize: 15, color: Colors.black)),
                  const SizedBox(height: 10),
                  FutureBuilder<PackageInfo>(
                    future: packageInfo,
                    builder: (context, snapshot) {
                      if (snapshot.hasData) {
                        return Text('Version ${snapshot.data!.version}', style: TextStyle(fontSize: 15, color: Colors.black));
                      }
                      return const CircularProgressIndicator();
                    },
                  ),
                ],
              ),
            ),
            ListView.builder(
              padding: const EdgeInsets.symmetric(horizontal: 15),
              physics: const NeverScrollableScrollPhysics(),  // 禁用 ListView 的滚动，交由外部控制
              shrinkWrap: true,  // 使 ListView 自适应大小
              itemCount: options.length,
              itemBuilder: (context, index) {
                return Container(
                  height: 65,
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: index == 0
                        ? BorderRadius.only(topLeft: Radius.circular(12), topRight: Radius.circular(12))
                        : index == options.length - 1
                            ? BorderRadius.only(bottomLeft: Radius.circular(12), bottomRight: Radius.circular(12))
                            : BorderRadius.zero,
                  ),
                  child: Column(
                    children: [
                      Expanded(
                        child: GestureDetector(
                          behavior: HitTestBehavior.opaque,
                          onTap: () {
                            if (index == 1) {
                              clickPush(context, UrlConfig.privacyPolicyURL(context));
                            } else if (index == 2) {
                              clickPush(context, UrlConfig.fuwuURL(context));
                            } else {
                              checkVersion(context);
                            }
                          },
                          child: Row(
                            children: [
                              SizedBox(width: 15),
                              Expanded(child: Text(options[index], style: const TextStyle(fontSize: 15, color: Color(0xFF333333)))),
                              Image.asset("assets/images/mineicon.png", width: 14, height: 14),
                              SizedBox(width: 15),
                            ],
                          ),
                        ),
                      ),
                      if (index != options.length - 1)
                        Padding(
                          padding: const EdgeInsets.symmetric(horizontal: 15),
                          child: const Divider(height: 1, color: Color(0xFFF5F5F5)),
                        ),
                    ],
                  ),
                );
              },
            ),
            SizedBox(height: 80), // 留出空间避免 UI 被遮挡
            
   
          ],
        ),
      ),
      bottomNavigationBar: Padding(
        padding: const EdgeInsets.only(bottom: 50),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            // 商业活动声明
            Text(
              "您通过本软件参加的商业活动，与AppleInc无关",
              style: TextStyle(
                color: Color(0xFF666666),
                fontSize: 15,
              ),
              textAlign: TextAlign.center,
            ),

            const SizedBox(height: 5),

            // ICP 备案
            GestureDetector(
              onTap: () => clickPush(context, "https://beian.miit.gov.cn"),
              child: Text(
                "豫ICP备2024082140号-2A >",
                style: TextStyle(
                  color: Color(0xFF666666),
                  fontSize: 15,
                ),
                textAlign: TextAlign.center,
              ),
            ),
          ],
        ),
      ),
    );
  }

  void clickPush(BuildContext context, String url) {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => WebViewPage(
          title: AppLocalizations.of(context)!.news,
          url: url,
        ),
      ),
    );
  }
}
